<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="dashboard.User"%>
<%@ page import="dashboard.Course"%>
<%@ page import="dashboard.Notification"%>
<%@ page import="dashboard.NotificationController"%>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<html>
<body>
	<div id="main">

		<%@include file="header.jsp"%>
		
		<%
		
			if(session.getAttribute("user") == null)
				response.sendRedirect("login.jsp");
			if (request.getParameter("I") != null) {
				String info = "";
				if (request.getParameter("I").equals("mail")) {
					info = "An E-mail has been sent to you.";
				}
				if (request.getParameter("I").equals("registered")) {
					info = "You have successfully registered, from now on you can login with the login button in the menu.";
				}
				if (request.getParameter("I").equals("logged")) {
					info = "You have successfully logged in.";
				}
				if (request.getParameter("I").equals("started")) {
					info = "The timer has started.";
				}
				if (request.getParameter("I").equals("favadded")) {
					info = "The user has been added to your favorites.";
				}
				if (request.getParameter("I").equals("favremoved")) {
					info = "The user has been removed from your favorites.";
				}
				if (request.getParameter("I").equals("alreadyfav")) {
					info = "This user is already in your favorites.";
				}

				if (request.getParameter("I").equals("miladded")) {
					info = "Your milestone has been created.";
				}

				session.setAttribute("info", info);
			}
		%>

		<%@include file="message.jsp"%>
		<%
			List<Notification> notifications = NotificationController.checkNotifications(currentUser);
			if (notifications!=null && notifications.size()!= 0) {
		%>
		
		<div class="title">Notifications</div>
		<table class='tablesorter' id='LinkTable' width='97%'>
				<thead>
					<tr>
						<th colspan="1">Type</th>
						<th colspan='1'>Courses</th>
						<th colspan='1'>Notification</th>
						<th colspan='1'>Act</th>
					</tr>
				</thead>
	
		<%
			
				for (Notification not : notifications) {
					String st="";

					if (not.getRole() == 1) {
				Milestone milestone = currentUser.getMilestoneById(not.getId());
					st = milestone.checkLength();
		%>
		<tr>
		<td>
		Milestone
		</td>
		<td>
		<%=st %>
		</td>
		<td>
		Milestone ends in less than 7 days!
		</td>
		<td><a href="milestone.jsp?action=viewspecific&id=<%=milestone.getId() %>">View</a></td>
		</tr>
		
		<%
			}

					else if (not.getRole() == 3) {
						Milestone milestone = currentUser.getMilestoneById(not.getId());
						st = milestone.checkLength();
						String fbName = "Milestone achieved!";
						String fbCaption = 
					"I have studied "+ milestone.getGoal() + " " + milestone.getAction() + " for "+ st+ " and therefore achieved the goal of my milestone :)!";
					session.setAttribute("fbName", fbName);
					session.setAttribute("fbCaption",fbCaption);
		%>
		<tr>
		<td>
		Milestone
		</td>
		<td>
		<%=st%>
		</td>
		<td>
		Milestone Achieved!<%@include file="facebook.jsp" %>
		</td>
				<td><a href="milestone.jsp?action=viewspecific&id=<%=milestone.getId() %>">View</a></td>
		</tr>
		
		<%
			}

					else if (not.getRole() == 4) {
						Milestone milestone = currentUser.getMilestoneById(not.getId());
					st = milestone.checkLength();
		%>
		<tr>
		<td>
		Milestone
		</td>
		<td>
		<%=st%>
		</td>
		<td>
		This milestone has ended, but wasn't achieved :(.
		</td>
				<td><a href="milestone.jsp?action=viewspecific&id=<%=milestone.getId() %>">View</a></td>
		</tr>
		<%
			} else {
		%>
		<tr>
		<td>
		Course
		</td>
		<td>
		<%=CourseRegistry.getInstance()
								.getCourseById(not.getId()).getCourseName()%>
		</td>
		<td>
		You are behind on this course!
		</td>
		</tr>
		<%
			}
				}
			}
		%>
		</tbody>
		</table>
		<div id="container">
			<div class="title">Course Progress:</div>

			<table class='tablesorter' id='LinkTable' width='97%'>
				<thead>
					<tr>
						<th colspan="1">Course</th>
						<th colspan='1'>total hours</th>
						<th colspan='1' style="width: 200px;">Progress</th>
					</tr>
				</thead>
				<tbody>
					<%
						for (Long crsId : currentUser.getCourses()) {
							double pcnt = Statistics.getPositionOfCourseStudyTime(currentUser, crsId);
							Course crs = CourseRegistry.getInstance().getCourseById(crsId);
					%>
					<tr>
						<td><%=crs.getCourseName()%></td>
		<% double duration = 100*Statistics.getCourseDurationUser(currentUser, crsId);
					    duration = Math.round(duration);
					    duration = duration / 100;
					    
					   double position =  Statistics.getPositionOfCourseStudyTime(currentUser, crsId);
					   double roundedPosition = position*100;
					   roundedPosition = Math.round(position);
					   
					    %>
						<td><%=duration%></td>
			
						<td class="progressbar" style="padding: 0px;"> <img src="img/progress-pointer.png" title="You're above <%=roundedPosition%> % of the total students for <%=CourseRegistry.getInstance().getCourseName(crsId)%>" 
							style="padding-left: <%=position - 5%>%; height: 50px;" ></td>
					</tr>
					<%
						}
					%>
					<tr>
						<td> Total: </td>
						<% double result = (Double) Statistics.getTotalDuration(currentUser, currentUser);
					    result = Math.round(result);%>
						<td><%=result%></td>
						<td class="progressbar" style="padding: 0px;"> <img src="img/progress-pointer.png" title="You're above <%=Math.round(Statistics.getPositionTotal(currentUser))%> % of the total students in total." 
							style="padding-left: <%=Statistics.getPositionTotal(currentUser) - 5%>%; padding-right: auto; height: 50px;" ></td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
</body>
</html>